//index.js
var app = getApp()
Page({
  data: {
    start:["☆","☆","☆","☆","☆"],
    saveImg:[],
    goods_id:'',
    order_sn:'',
    data:{}
  },
  onLoad: function(op) {
    var that = this;
    var id = op.id;
    var sn = op.sn;
    console.log(op.id+'+'+op.sn);
    wx.request({
       url: app.url+'?controller=api_comment&action=getGoodsPartMsg',
       header: {
                'content-type': 'application/json',
                'token':wx.getStorageSync('token_key')
              }, 
      data: {
        goods_id:id,
        order_id:sn,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var goods = res.data.message[0];
        var img = app.urlimg+goods.img;
        var title = goods.name;
        var price = goods.sell_price;

        var data = that.data;
        data['img'] = img;
        data['title'] = title;
        data['price'] = price;

        that.setData({
          goods_id:id,
          order_sn:sn,
          data:data,
        })
        console.log(that.data);
        
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
    //Do some initialize when page load.
    
  },

    // 响应选择星星
  startTap: function(e) {

    var that = this;

    var start = that.data.start;
    
    
    var index = e.currentTarget.id;

    for(var i =0 ;i < start.length; i++) {
      if(i <= index){
        start[i] = "★";
      }else {
        start[i] = "☆";
      }
    }

    that.setData({
      start:start,
      selectStart:index,
    });

  
},
addImageTap:function(e) {

var that = this;

wx.chooseImage({
  count: 5, // 默认9
  sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
  success: function (res) {
    
    console.log(res);
    // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
    var img = res.tempFilePaths;
    console.log(img);
    that.setData({
      images:img,
      saveImg:img,
    });
  }
})

}
,
// 响应提交评论数据
submitTap: function(e) {

  var that = this;
  var value = e.detail.value;

  var images = that.data.saveImg;

  if(images.length  > 0) {

       wx.showToast({
        title: '正在上传图片...',
        icon: 'loading',
      });

      var index=0;
    for(var i = 0 ; i < images.length; i++) {
         wx.uploadFile({
        url:app.url+'?controller=api_comment&action=comment_img_upload',
        formData:{
          goods_id:that.data.goods_id,
          order_no:that.data.order_sn,
          'token':wx.getStorageSync('token_key')
        },
        filePath:images[i],
        name:'file'
        ,complete:function(){
          index ++;
          if(index == images.length -1) {
               submitComment(that,value);
          }
        }
      })
    }
  }

  if(images.length < 1) {
    //调用 评论方法
    submitComment(that,value);
  }
  
    
},
})

// 提交评论
function submitComment(that,value){
  if(value.comment.length < 1 && value.start.length < 1) {
   wx.showModal({
            title:'提示',
            content:"评论和相符不能为空噢。",
          })
    return;
  }
  wx.hideToast();
   wx.showToast({
    title: '提交评论...',
    icon: 'loading',
  });
   wx.request({
        url: app.url+'?controller=api_comment&action=addComment',
        header:{
          'content-type': 'application/json',
        },
        data: {
          goods_id:that.data.goods_id,
          order_no:that.data.order_sn,
          contents:value.comment,
          point:value.start,
          'token':wx.getStorageSync('token_key')

        },
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          //  var toStr = JSON.stringify(res);
          //     console.log('res'+toStr);
          //    这里偷懒没判断提交结果

          wx.hideToast();
          wx.showModal({
            title:'评论提交成功',
            content:"评论提交成功了",
            success() {
                wx.navigateBack({
                  delta: 1, // 回退前 delta(默认为1) 页面
                  complete:function(e){
                    console.log('e='+e);
                  }
                })
            },
          })
          
        }
      })

}
